<?
if (GetParam("CPID","") && (GetParam("CPOPR","") || GetParam("StartUp",""))) {
	$SQL = "SELECT * FROM $DBCoins.coins_container_process WHERE CPID='".GetParam("CPID","")."'";
	$Result = $DBConnection->dbc->get_row($SQL);
	if (GetParam("StartUp","")) {
		$js_script  = "document.ReceiptForm.vessel.value 		= '".$Result->CPIVES."';\n";
		$js_script .= "document.ReceiptForm.voyage.value 		= '".$Result->CPIVOY."';\n";
		$js_script .= "document.ReceiptForm.customer.value 	= '".$Result->CPCUST."';\n";
		$js_script .= "document.ReceiptForm.principal.value = '".$Result->CPOPR."';\n";
		$js_script .= "document.ReceiptForm.do_number.value = '".$Result->CPIORDERNO."';\n";
		$js_script .= "document.ReceiptForm.cpid.value 			= '".$Result->CPID."';\n";	
	} else {
		$js_script  = "window.opener.document.ReceiptForm.vessel.value 		= '".$Result->CPIVES."';\n";
		$js_script .= "window.opener.document.ReceiptForm.voyage.value 		= '".$Result->CPIVOY."';\n";
		$js_script .= "window.opener.document.ReceiptForm.customer.value 	= '".$Result->CPCUST."';\n";
		$js_script .= "window.opener.document.ReceiptForm.do_number.value = '".$Result->CPIORDERNO."';\n";
		$js_script .= "window.opener.document.ReceiptForm.cpid.value 			= '".$Result->CPID."';\n";
		$js_script .= "self.close();\n";	
	}
	print $js_script;
	die();	
}
?>
<html>
<script type="text/javascript" src="<?=$ROOT_JS_DIR?>/common.js"></script>
<script type="text/javascript" src="<?=$ROOT_JS_DIR?>/jsmasks.js"></script>
<body>
<h1>Loading data...</h1>
<?

define("PRIMARY_KEY","nik");
$FileName       = "ais_transaction_receipt_container_rpc.php";
$HTMLFormName   = "ReceiptForm";

if (GetParam("tglbkt","") && GetParam("curr","")) {
	list($day,$month,$year) = explode("/",GetParam("tglbkt",""));
	$SQL = "SELECT rate FROM ais_kurssi WHERE tglbkt <= '$year-$month-$day' AND curr='".GetParam("curr","")."' ORDER BY tglbkt DESC LIMIT 1";
//	print $SQL;
	$rate = $DBConnection->dbc->get_var($SQL);
	if ($rate) {
		$js_script  = "<script>\n";
		$js_script .= "  parent.document.".$HTMLFormName.".rate.value = '$rate';\n";		
		$js_script .= "  oMask = new Mask('#,###.#0', 'number');\n";
		$js_script .= "  parent.document.".$HTMLFormName.".rate.value = oMask.format(parent.document.$HTMLFormName.rate.value);\n";
		$js_script .= "</script>\n";		
		print $js_script;		
	} else {
		$js_script  = "<script>\n";
		$js_script .= "  parent.document.".$HTMLFormName.".rate.value = '0';\n";		
		$js_script .= "  oMask = new Mask('#,###.#0', 'number');\n";
		$js_script .= "  parent.document.".$HTMLFormName.".rate.value = oMask.format(parent.document.$HTMLFormName.rate.value);\n";
		$js_script .= "</script>\n";		
		print $js_script;		
	}
} elseif (GetParam("thbl","") && GetParam("idx","")) {
	$SQL   = "SELECT skada,receiptname FROM tblreceipt WHERE kdreceipt=".GetParam("thbl","");
	$res = $DBConnection->dbc->get_row($SQL);
	if (strlen($skada)) {
		$js_script  = "<script>\n";
		$js_script .= "  parent.document.$HTMLFormName.skada".GetParam("idx","").".value = '".$res->skada."'\n";
		$js_script .= "  parent.document.$HTMLFormName.uraian".GetParam("idx","").".value = '".$res->receiptname."'";
		$js_script .= "</script>\n";
	} else {
		$js_script  = "<script>\n";
		$js_script .= "  parent.document.$HTMLFormName.skada".GetParam("idx","").".value = '".$res->skada."'\n";
		$js_script .= "  parent.document.$HTMLFormName.uraian".GetParam("idx","").".value = '".$res->receiptname."'";
		$js_script .= "</script>\n";		
	}
	print $js_script;
} elseif (GetParam("ledger","") && GetParam("trsto","")) {
		list($day,$month,$year) = explode("/",GetParam("ledger",""));
	    $SQL   = "SELECT count(*) FROM ais_history_ledger WHERE month='$month' AND year = '$year' AND status='open'";
	    $ExistPeriod = $DBConnection->dbc->get_var($SQL);
		if (!$ExistPeriod) {
			$js_script  = "<script>\n";
			$js_script .= "  parent.document.$HTMLFormName.".GetParam("trsto","").".value = '';\n";
			$js_script .= "  alert('Invalid Date, Period Allready Closed!');\n";
			$js_script .= "  parent.document.$HTMLFormName.".GetParam("trsto","").".focus();\n";
			$js_script .= "</script>\n";
	    }
	print $js_script;	    
} elseif (GetParam("crno","")) {
	$SQL = "
			SELECT $DBCoins.`coins_container_process`.`CPCUST`,$DBCoins.`coins_container_process`.`CPOPR`,$DBCoins.`coins_container_process`.`CPID`
						 $DBCoins.`coins_container_process`.`CPIVES`,
						 $DBCoins.`coins_container_process`.`CPIVOY`
			FROM
			  $DBCoins.`tblcontainer`
			  INNER JOIN $DBCoins.`coins_container_process` ON (`tblcontainer`.`CRCPID` = `coins_container_process`.`CPID`)	
			WHERE $DBCoins.`tblcontainer`.`CRNO` = '".GetParam("crno","")."'
	";
	//print $SQL;
	$res = $DBConnection->dbc->get_row($SQL);
		if ($res) {
			$js_script  = "<script>\n";
			$js_script .= "  parent.document.$HTMLFormName.principal.value = '".$res->CPIOPR."';\n";
			$js_script .= "  parent.document.$HTMLFormName.customer.value = '".$res->CPICUST."';\n";
			$js_script .= "  parent.document.$HTMLFormName.cpid.value = '".$res->CPID."';\n";
			$js_script .= "  parent.document.$HTMLFormName.vessel.value = '".$res->CPIVES."';\n";
			$js_script .= "  parent.document.$HTMLFormName.voyage.value = '".$res->CPIVOY."';\n";
			$js_script .= "</script>\n";
	  }
	print $js_script;	    	  
} elseif (GetParam("princ","")) {
	$SQL = "
			SELECT *
			FROM $DBCoins.`tblprincipal`			  
			WHERE $DBCoins.`tblprincipal`.`PRCODE` = '".GetParam("prcode","")."'
	";
	//print $SQL;
	$res = $DBConnection->dbc->get_row($SQL);
		if ($res) {
			$js_script  = "<script>\n";
			$js_script .= "  parent.document.$HTMLFormName.principal.value = '".$res->CPIOPR."';\n";
			$js_script .= "  parent.document.$HTMLFormName.customer.value = '".$res->CPICUST."';\n";
			$js_script .= "  parent.document.$HTMLFormName.vessel.value = '".$res->CPIVES."';\n";
			$js_script .= "  parent.document.$HTMLFormName.voyage.value = '".$res->CPIVOY."';\n";
			$js_script .= "</script>\n";
	  }
	
	print $js_script;
}

?>